<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.username" placeholder="名称" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">新增</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      ref="categoryRef"
      border
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      row-key="categoryId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @row-click="handleRowClick"
    >
      <el-table-column show-overflow-tooltip min-width="100" align="left" prop="categoryName" label="名称"></el-table-column>
      <el-table-column show-overflow-tooltip min-width="100" align="left" prop="categoryCode" label="类别标识"></el-table-column>
      <el-table-column show-overflow-tooltip width="50" align="center" prop="sortNum" label="排序"></el-table-column>
      <el-table-column show-overflow-tooltip min-width="150" align="center" prop="beanName" label="类服务名"></el-table-column>
      <el-table-column show-overflow-tooltip min-width="200" align="center" prop="beanClass" label="类包名"></el-table-column>
      <el-table-column show-overflow-tooltip min-width="150" align="center" prop="remark" label="备注"></el-table-column>
      <el-table-column show-overflow-tooltip width="100" align="center" prop="createdBy" label="创建人"></el-table-column>
      <el-table-column show-overflow-tooltip width="160" align="center" prop="createdTime" label="创建时间"></el-table-column>
      <el-table-column width="100" align="center" label="操作">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="编辑当前行" placement="top">
            <el-button icon="el-icon-edit" type="primary" size="mini" circle @click="handleEdit(row)"></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除当前行" placement="top">
            <el-button icon="el-icon-delete" type="danger" size="mini" :disabled="hasChildren(row)" circle @click="handleDelete(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/workflow/modelCategory'
  import Edit from './components/Edit'
  import { handleTree } from '@/utils/index'
  export default {
    name: 'ModelCategory',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        elementLoadingText: '正在加载...',
        queryForm: {},
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      hasChildren(row) {
        if (row.children) {
          return true
        }
        return false
      },
      handleRowClick(row, event, column) {
        this.$refs.categoryRef.toggleRowExpansion(row)
      },
      handleEdit(row) {
        if (row.categoryId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await doDelete(row.categoryId).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = handleTree(data, 'categoryId', 'parentId', 'children', 0)
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
